/************************************************************************************/
/*  Program name CAPPOIMP_ORGANIZATION_STRUCTURE.sql                                */
/*  Creater:  CHAN KA KEUNG BILLY (PCCW - UniHub)                                   */
/*************   History   *************************                                */
/* 09, MAY 2006 CHAN KA KEUNG BILLY     First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FND_CONCURRENT_PROGRAMS_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPPOIMP_ORGANIZATION_STRUCTURE';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_PO_ORGANIZATION_STRUCTURE;

		lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME
	UPDATE
		PER_ORGANIZATION_STRUCTURES
	SET		
		NAME = CAPBRIMP_MAPPING.RenameStr(SUBSTR(NAME,1,4), NAME)
	WHERE	ORGANIZATION_STRUCTURE_ID	IN	(
								SELECT
									ORGANIZATION_STRUCTURE_ID
								FROM
									CRST_PO_ORGANIZATION_STRUCTURE
								);
	

	FOR C1 IN Cur_F LOOP
				
		BEGIN
				
			SELECT PER_ORGANIZATION_STRUCTURES_S.NEXTVAL
			INTO lv_Next_Seq_ID
			FROM DUAL;

		     update CRST_PO_ORGANIZATION_STRUCTURE
		     set    ORGANIZATION_STRUCTURE_ID_N = lv_Next_Seq_ID
		     where  ORGANIZATION_STRUCTURE_ID = C1.ORGANIZATION_STRUCTURE_ID;

			INSERT INTO PER_ORGANIZATION_STRUCTURES
			VALUES
			(
			lv_Next_Seq_ID									,
			CAPBRIMP_MAPPING.GetBGMapping(C1.BUSINESS_GROUP_ID),
			C1.NAME                           ,
			C1.COMMENTS                       , 
			C1.PRIMARY_STRUCTURE_FLAG         ,
			C1.REQUEST_ID                     ,
			C1.PROGRAM_APPLICATION_ID         ,
			C1.PROGRAM_ID                     ,
			C1.PROGRAM_UPDATE_DATE            ,
			C1.ATTRIBUTE_CATEGORY             ,
			C1.ATTRIBUTE1                     ,
			C1.ATTRIBUTE2                     ,
			C1.ATTRIBUTE3                     ,
			C1.ATTRIBUTE4                     ,
			C1.ATTRIBUTE5                     ,
			C1.ATTRIBUTE6                     ,
			C1.ATTRIBUTE7                     ,
			C1.ATTRIBUTE8                     ,
			C1.ATTRIBUTE9                     ,
			C1.ATTRIBUTE10                    ,
			C1.ATTRIBUTE11                    ,
			C1.ATTRIBUTE12                    ,
			C1.ATTRIBUTE13                    ,
			C1.ATTRIBUTE14                    ,
			C1.ATTRIBUTE15                    ,
			C1.ATTRIBUTE16                    ,
			C1.ATTRIBUTE17                    ,
			C1.ATTRIBUTE18                    ,
			C1.ATTRIBUTE19                    ,
			C1.ATTRIBUTE20                    ,
			C1.LAST_UPDATE_DATE               ,
			C1.LAST_UPDATED_BY                ,
			C1.LAST_UPDATE_LOGIN              ,
			C1.CREATED_BY                     ,
			C1.CREATION_DATE                  ,
			C1.POSITION_CONTROL_STRUCTURE_FLG ,
			C1.OBJECT_VERSION_NUMBER           
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER FND_CONCURRENT_PROGRAMS_CT1 ENABLE;
